Ext.namespace("GridBuildingInfo");

GridBuildingInfo.List=function(){
	
	this.detailPanel = new GridBuildingInfo.Detail(this);
	
	this.txtBulidingName = new Ext.form.TextField({
		width:100
	});
	
	this.txtBulidingAddress = new Ext.form.TextField({
		width:150
	});
		
//	this.cmbBuildingRegion = new Ext.form.ComboBox({
//		width :120,
//		store : new Ext.data.JsonStore({
//		   url:"../gridareas/listAllRegions.form",
//		   autoLoad : true,
//		   root:"regions",
//		   fields:["areaId","name"]
//		}),
//		valueField : 'areaId',
//		editable: false,
//		displayField :'name',
//      mode: 'local',
//		typeAhead :true,
//		triggerAction :'all',
//		emptyText :'请选择所在区域',
//		selectOnFocus :true	
//	})
	
	this.cmbBuildingType = new Ext.form.ComboBox({
        width: 120,
        //fieldLabel: '楼宇类别',
        store: OMS.GRIDBUILDING_TYPE,
        typeAhead:true,
        editable: false,
        mode: 'local',
        triggerAction: 'all',
        emptyText:'请选择楼宇类别',
        selectOnFocus: true
    });
    
    //删除
     this.btnDelete = new Ext.Toolbar.Button({//btnDelete
        iconCls: 'icon-delete',
        text: '删除',
        id:'deleteBuilding',
        //disabled:true,
        tooltip: {text: '删除楼宇'},
        handler: this.onDelete,
        scope: this
    });
    
    //删除
    this.btnSearch = new Ext.Toolbar.Button({
        iconCls: 'icon-search',
        text: '查询',
        tooltip: {text: '查询楼宇'},
        handler: this.onSearch,
        scope: this
    });
    
    this.store = new Ext.data.Store({
		proxy: new Ext.data.HttpProxy({
			url: '../gridbuildinginfo/list.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		}),
		reader: new Ext.data.JsonReader({
			root: 'buildinginfos',
			totalProperty: 'total',
			id: 'buildingId',
			fields: ['buildingId','buildingName','addressDetail',
			'buildingType','buildingGrade','constructionFinishDateStr','businessArea','buildingDesc','buildingNum','areaName','gridName']
		}),
		listeners:{
			 beforeload:{
				fn:function(thiz,options){
					Ext.apply(thiz.baseParams,{
						name:this.txtBulidingName.getValue(),
						address:this.txtBulidingAddress.getValue(),
//						partition:this.cmbBuildingRegion.getValue(),
						type:this.cmbBuildingType.getValue()
					});
				},
				scope:this
			}
		}
	});
	
	this.colModel = new Ext.grid.ColumnModel({
		defaults:{
			width:40,
			sortable:true,
			align:'center'
		},
		columns:[
			new Ext.grid.RowNumberer(),
			{id:'buildingId',header:'楼宇编号',dataIndex:'buildingId',hidden:true},
			{header:'楼宇名称',dataIndex:'buildingName',width:80},
			{header:'楼宇地址',dataIndex:'addressDetail',width:100},
			{header:'所属网格',dataIndex:'gridName',width:100},
			{header:'所属区域',dataIndex:'areaName',width:100},
			{header:'楼宇类别',dataIndex:'buildingType',renderer:this.renderTypes,width:70},
			{header:'楼宇档次',dataIndex:'buildingGrade',width:80,renderer:this.renderGrade},
			{header:'竣工日期',dataIndex:'constructionFinishDateStr',width:100}
		]
	});
	
	GridBuildingInfo.List.superclass.constructor.call(this,{
		title: '楼宇列表',
		region: 'center',
		tbar: ['楼宇名称:',this.txtBulidingName,'&nbsp;楼宇地址:',this.txtBulidingAddress,'&nbsp;楼宇类别:',this.cmbBuildingType,'-',this.btnSearch],
		sm: new Ext.grid.RowSelectionModel({singleSelect:true}),
		loadMask: {msg:'加载中，请稍候...'},
		bbar:new Ext.PagingToolbar({
			pageSize:15,
			store: this.store,
			displayInfo: true,
			displayMsg: '显示第 {0} 条到 {1} 条网格信息，一共 {2} 条',
			emptyMsg: '当前没有楼宇记录'
		}),
		viewConfig:{forceFit:true},
		listeners: {
            render: function() {
//				new Ext.Toolbar({
//	                renderTo: this.tbar,
//	                items: [//'所在区域:',this.cmbBuildingRegion,
//	                		   '楼宇类别:',this.cmbBuildingType,'-',this.btnSearch ]
//	            });
				new Ext.Toolbar({
					renderTo: this.tbar,
					id : 'buildingToolBar',
					items: [{text:'增加楼宇', scope:this, iconCls:'icon-add', handler:this.onAdd},
							{text:'修改楼宇', scope:this, iconCls:'icon-modify', handler:this.onModify},
						     this.btnDelete]
				});
			}
		}
	});
	this.on('dblclick',this.onModify,this);
	this.store.load();
	

};

Ext.extend(GridBuildingInfo.List,Ext.grid.GridPanel,{
	onSearch:function(){
		this.store.load({
			params:{
				start:0,
				limit:15,
				name:this.txtBulidingName.getValue(),
				address:this.txtBulidingAddress.getValue(),
//				regionId:this.cmbBuildingRegion.getValue(),
				type:this.cmbBuildingType.getValue()
			}
		});
	},
	renderTypes: function(value, attr, record) {
		if(value !=null&&value !=''){
			return OMS.GRIDBUILDING_TYPE[value][1];
		}
		return '';
	},
	renderGrade: function(value, attr, record) {
		if(value !=null&&value !=''){
			return OMS.GRIDBUILDING_GRADE[value][1];
		}
		return '';
	},
	renderBusinessArea: function(value, attr, record) {
		if(value !=''&&value<OMS.GRIDBUILDING_BUSINESSAREA.length){
			return OMS.GRIDBUILDING_BUSINESSAREA[value][1];
		}
		return '';
	},

	update:function(){
		this.store.reload();
	},
	onAdd: function(btn, event) {
		if (this.detailPanel == null) {
			this.detailPanel = new GridBuildingInfo.Detail(this);
		}
		this.detailPanel.show();
		this.detailPanel.initData();
	},
	onDelete: function(btn, event) {		
		var connDelete = new Ext.data.Connection({
			url: '../gridbuildinginfo/delete.form?timesnap=' + Date.parse(new Date()),
			method: 'POST'
		});
		
		var record = this.getSelectionModel().getSelected();
		if(record!=null){
			//Ext.MessageBox.confirm("提示","是否确认删除？");
			Ext.MessageBox.confirm("提示","确认删除  '"+record.data.buildingName+"' ？",function(btn){
            if(btn=="yes"){
                //alert("yes");
                Utils.request(connDelete, {
					params: {
						'buildingId': record.data.buildingId
					},
					scope: this,
					success: function(resp, action) {
						//this.scope.update();
						this.scope.store.load();
						//this.scope.hide();
					},
					after: function() {}
				});
            }   
            else{
               }
			},this);
			
		}else{		
			Ext.MessageBox.alert('提示','请先选中一条记录!');
		}
	},
	onModify: function(btn, event) {
		if (this.detailPanel == null) {
			this.detailPanel = new GridBuildingInfo.Detail(this);
		}
		var record = this.getSelectionModel().getSelected();
		if(record!=null){
			this.detailPanel.show();
			this.detailPanel.initData(record.data.buildingId);
		}
	}
});

//edit for callback from gis_for_management.jsp  by JimmyLee start
//@see grid-building-details.js
var lister = null;
GridBuildingInfo.main=function(){
//edit by JimmyLee end 	
	lister = new GridBuildingInfo.List();
	
	var mainview = new Ext.Viewport({
		layout:'border',
		items:[lister]
	});
};

Ext.onReady(function(){
	Ext.QuickTips.init();
	Ext.form.Field.prototype.msgTarget='side';
	Ext.BLANK_IMAGE_URL='../resources/images/default/s.gif';
	GridBuildingInfo.main();
	var connSyncSession = new Ext.data.Connection({
			url : '../security/getSessionUser.form?timesnap=' + Date.parse(new Date()),
			method : 'POST'
		});
		Utils.request(connSyncSession, {
			success : function(resp, action) {
				var name = (resp.user)?resp.user.userId:'';
//				if(name=="admin"){
//					Ext.getCmp("gisForm").enable();
//					Ext.getCmp("deleteBuilding").enable();
//				}
				
			}
		});
	
});
